home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / ja91 / ASCII / 68040 / 68040.txt
Encoding:
Text File  |  1991-08-09  |  16.9 KB  |  428 lines

  1. (c)  Copyright 1991 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice,
  3. and is provided "as is" without warranty of any kind, either expressed
  4. or implied.  The entire risk as to the use of this information is
  5. assumed by the user.
  6.  
  7.  
  8.  
  9. 68040 Compatibility Warning
  10.  
  11.  
  12. by Michael Sinz
  13.  
  14. editor's note: the new exec.library routines referred to in this article are
  15. part of the 2.04 OS release.
  16.  
  17. Now that Motorola's 68040รค CPU is available, it will not be long before it
  18. appears in Amiga-based products.  In fact, many Amiga magazines already have
  19. ads for 3rd party 68040 CPU cards.
  20.  
  21. The 68040 is a much more powerful CPU than its predecessors.  It has 4K of
  22. cache memory for instructions and another 4K cache for data.  The reason for
  23. these two separate caches is so that the CPU core can access data and CPU
  24. instructions at the same time.
  25.  
  26. Although the 68040 provides greater performance it also brings with it
  27. greater compatibility problems.  Just the fact that the caches are so much
  28. larger than Motorola's 68030 CPU can cause problems.  However, this is not
  29. its biggest obstacle.
  30.  
  31. The 68040 data cache has a mode that can make the system run much faster in
  32. most cases.  It is called CopyBack mode.  When a program writes data to
  33. memory in this mode, the data goes into the cache but not into the physical
  34. RAM.  That means that if a program or a piece of hardware were to read that
  35. RAM without going through the data cache on the 68040, it will read old data.
  36. CopyBack mode effects two areas of the Amiga: DMA devices and the CPU's
  37. instruction reading.
  38.  
  39. CopyBack mode effects DMA devices because they read and write data directly
  40. to memory.  Using DMA with CopyBack mode requires a cache flush.  If a DMA
  41. device needs to read RAM via DMA, it must first make sure that data in the
  42. caches has been written to memory.  It can do this by calling the Exec
  43. function CachePreDMA().  If a DMA device is about to write to memory, it
  44. should call CachePreDMA() before the write, do the DMA write, and then call
  45. CachePostDMA(), which  makes sure that the CPU uses the data just written to
  46. memory.
  47.  
  48. An added advantage of using the CachePreDMA() and CachePostDMA() functions is
  49. that they give the OS the chance to tell the DMA device that the physical
  50. addresses and memory sizes are not the same.  This will make it possible in
  51. the future to add features such as virtual memory.  See the Autodocs for more
  52. information on these calls (the Autodocs have been included in this article).
  53. The other major compatibility problem with the 68040's CopyBack mode is with
  54. fetching CPU instructions.  CPU instructions have to be loaded into memory so
  55. the CPU can copy them into its instruction cache.  Normally, instructions
  56. that will beexecuted are written to memory by the CPU (i.e. loading a program
  57. from disk).  In CopyBack mode, anything the CPU writes to memory, including
  58. CPU instructions, doesn't actually go into memory, it goes into the data
  59. cache.  If instructions are not flushed out of the data cache to RAM, the
  60. 68040 will not be able to find them when it tries to copy them into the
  61. instruction cache for execution.  It will instead find and attempt to execute
  62. whatever garbage data happened to be left at that location in RAM.
  63.  
  64. To remedy this, any program that writes instructions to memory must flush the
  65. data cache after writing.  The V37 Exec function CacheClearU() takes care of
  66. this.  Release 2.0 of the Amiga OS correctly flushes the caches as needed
  67. after it does the LoadSeg() of a program (LoadSeg() loads Amiga executable
  68. programs into memory from disk).  Applications need to do the same if they
  69. write code to memory.  One such example was the article ``Creating Multiple
  70. Processes with Re-entrant Code'' from the November/December 1989 issue of
  71. Amiga Mail (Volume I).  For that example to work with the 68040's CopyBack
  72. mode, it needs to flush the cache before it asks the system to execute the
  73. code it wrote to memory (which may still be in the cache).  It can do that by
  74. calling CacheClearU() before the call to CreateProc().  In C that would be:
  75.  
  76. extern struct ExecBase *SysBase;
  77.  
  78. . . .
  79.  
  80. /* If we are in 2.0, call CacheClearU() before CreateProc() */
  81. if (SysBase->LibNode.lib_Version >= 37) CacheClearU();
  82.  
  83. /* Now do the CreateProc() call... */
  84. proc=CreateProc(... /* whatever your call is like */ ...);
  85.  
  86. . . .
  87.  
  88.  
  89. For those of you programming in assembly:
  90.  
  91. *******************************************************************************
  92. * Check to see if we are running in V37 ROM or better.  If so,
  93. * we want to call CacheClearU() to make sure we are safe on future
  94. * hardware such as the 68040.  This section of code assumes that
  95. * a6 points at ExecBase.  a0/a1/d0/d1 are trashed in CacheClearU()
  96. *
  97.                 cmpi.w  #37,LIB_VERSION(a6)     ; Check if exec is >= V37
  98.                 bcs.s   TooOld                  ; If less than V37, too old...
  99.                 jsr     _LVOCacheClearU(a6)     ; Clear the cache...
  100. TooOld:
  101. *
  102. *******************************************************************************
  103.  
  104. Note that CreateProc() is not the only routine where CopyBack mode could be a
  105. problem.  Any program that copies code into memory for execution that is not
  106. done via LoadSeg() (for example, ``Using SetFunction() in a Debugger'' from
  107. the March/April 1991 issue of Amiga Mail), you will need to call
  108. CacheClearU().  Many input.device handlers have been known to allocate and
  109. copy the handler code into memory and then exit back to the system.  These
  110. programs also need to have this call in them.  The above code will work under
  111. pre-2.0 versions of the OS, and will do the correct operations in 2.04 (and
  112. beyond).
  113.  
  114. Heeding these compatibility guidelines will allow the system to enable
  115. CopyBack mode, enhancing the performance of the Amiga considerably.  Ignoring
  116. these guidelines will prevent the Amiga from taking advantage of CopyBack
  117. mode and possibly other advanced features of the 68040 (and beyond).
  118.  
  119.  
  120. exec.library/CacheControl                           exec.library/CacheControl
  121.  
  122.    NAME
  123.         CacheControl - Instruction & data cache control
  124.  
  125.    SYNOPSIS
  126.         oldBits = CacheControl(cacheBits,cacheMask)
  127.         D0                     D0        D1
  128.  
  129.         ULONG CacheControl(ULONG,ULONG);
  130.  
  131.    FUNCTION
  132.         This function provides global control of any instruction or data
  133.         caches that may be connected to the system.  All settings are
  134.         global -- per task control is not provided.
  135.  
  136.         The action taken by this function will depend on the type of
  137.         CPU installed.  This function may be patched to support external
  138.         caches, or different cache architectures.  In all cases the function
  139.         will attempt to best emulate the provided settings.  Use of this
  140.         function may save state specific to the caches involved.
  141.  
  142.         The list of supported settings is provided in the exec/execbase.i
  143.         include file.  The bits currently defined map directly to the Motorola
  144.         68030 CPU CACR register.  Alternate cache solutions may patch into
  145.         the Exec cache functions.  Where possible, bits will be interpreted to
  146.         have the same meaning on the installed cache.
  147.  
  148.    INPUTS
  149.         cacheBits - new values for the bits specified in cacheMask.
  150.  
  151.         cacheMask - a mask with ones for all bits to be changed.
  152.  
  153.    RESULT
  154.         oldBits   - the complete prior values for all settings.
  155.  
  156.    NOTE
  157.         As a side effect, this function clears all caches.
  158.  
  159.    Selected bit definitions for Cache manipulation calls from <exec/execbase.i>
  160.  
  161.         BITDEF  CACR,EnableI,0          ;Enable instruction cache
  162.         BITDEF  CACR,FreezeI,1          ;Freeze instruction cache
  163.         BITDEF  CACR,ClearI,3           ;Clear instruction cache
  164.         BITDEF  CACR,IBE,4              ;Instruction burst enable
  165.         BITDEF  CACR,EnableD,8          ;68030 Enable data cache
  166.         BITDEF  CACR,FreezeD,9          ;68030 Freeze data cache
  167.         BITDEF  CACR,ClearD,11          ;68030 Clear data cache
  168.         BITDEF  CACR,DBE,12             ;68030 Data burst enable
  169.         BITDEF  CACR,WriteAllocate,13   ;68030 Write-Allocate mode (must
  170.                                         ;always be set)
  171.         BITDEF  CACR,EnableE,30
  172.     *
  173.     *   With this bit set, the external cache states then match the internal
  174.     *   cache states.  That is, if the internal data cache is turned on,
  175.     *   the external data cache is turned on.  Same for instruction caches.
  176.     *   If the external cache only exists as a single unified cache,
  177.     *   this will turn on if the data cache is on.  (This is done for
  178.     *   compatibility reasons)  With this bit clear, the external caches
  179.     *   are turned off.
  180.  
  181.         BITDEF  CACR,CopyBack,31    ;Master enable for copyback caches
  182.  
  183.  
  184.         BITDEF  DMA,Continue,1      ;Continuation flag for CachePreDMA
  185.         BITDEF  DMA,NoModify,2      ;Set if DMA does not update memory
  186.  
  187.  
  188.    SEE ALSO
  189.         exec/execbase.i, CacheClearU, CacheClearE
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. exec.library/CacheClearE                             exec.library/CacheClearE
  199.  
  200.    NAME
  201.         CacheClearE - Cache clearing with extended control (V37)
  202.  
  203.    SYNOPSIS
  204.         CacheClearE(address,length,caches)
  205.                     a0      d0     d1
  206.  
  207.         void CacheClearE(APTR,ULONG,ULONG);
  208.  
  209.    FUNCTION
  210.         Flush out the contents of the CPU instruction and/or data caches.
  211.         If dirty data cache lines are present, push them to memory first.
  212.  
  213.         Motorola CPUs have separate instruction and data caches.  A data
  214.         write does not update the instruction cache.  If an instruction is
  215.         written to memory or modified, the old instruction may still exist
  216.         in the cache.  Before attempting to execute the code, a flush of
  217.         the instruction cache is required.
  218.  
  219.         For most systems, the data cache is not updated by Direct Memory
  220.         Access (DMA), or if some external factor changes shared memory.
  221.  
  222.         Caches must be cleared after *any* operation that could cause
  223.         invalid or stale data.  The most common cases are DMA and modifying
  224.         instructions using the processor.
  225.  
  226.         Some examples:
  227.                         Self modifying code
  228.                         Building Jump tables
  229.                         Run-time code patches
  230.                         Relocating code for use at different addresses.
  231.                         Loading code from disk
  232.  
  233.    INPUTS
  234.         address - Address to start the operation.  This may be rounded
  235.                   due to hardware granularity.
  236.         length  - Length of area to be cleared, or $FFFFFFFF to indicate all
  237.                   addresses should be cleared.
  238.         caches  - Bit flags to indicate what caches to affect.  The current
  239.                   supported flags are:
  240.                         CACRF_ClearI    ;Clear instruction cache
  241.                         CACRF_ClearD    ;Clear data cache
  242.                   All other bits are reserved for future definition.
  243.  
  244.    NOTES
  245.         On systems with a copyback mode cache, any dirty data is pushed
  246.         to memory as a part of this operation.
  247.  
  248.         Regardless of the length given, the function will determine the most
  249.         efficient way to implement the operation.  For some cache systems,
  250.         including the 68030, the overhead partially clearing a cache is often
  251.         too great.  The entire cache may be cleared.
  252.  
  253.         For all current Amiga models, Chip memory is set with Instruction
  254.         caching enabled, data caching disabled.  This prevents coherency
  255.         conflicts with the blitter or other custom chip DMA.  Custom chip
  256.         registers are marked as non-cacheable by the hardware.
  257.  
  258.         The system takes care of appropriately flushing the caches for normal
  259.         operations.  The instruction cache is cleared by all calls that
  260.         modify instructions, including LoadSeg(), MakeLibrary() and
  261.         SetFunction().
  262.  
  263.    SEE ALSO
  264.         exec/execbase.i, CacheControl, CacheClearU
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. exec.library/CacheClearU                             exec.library/CacheClearU
  282.  
  283.    NAME
  284.         CacheClearU - User callable simple cache clearing (V37)
  285.  
  286.    SYNOPSIS
  287.         CacheClearU()
  288.  
  289.         void CacheClearU();
  290.  
  291.    FUNCTION
  292.         Flush out the contents of any CPU instruction and data caches.
  293.         If dirty data cache lines are present, push them to memory first.
  294.  
  295.         Caches must be cleared after *any* operation that could cause
  296.         invalid or stale data.  The most common cases are DMA and modifying
  297.         instructions using the processor.  See the CacheClearE() autodoc
  298.         for a more complete description.
  299.  
  300.         Some examples of when the cache needs clearing:
  301.                         Self modifying code
  302.                         Building Jump tables
  303.                         Run-time code patches
  304.                         Relocating code for use at different addresses.
  305.                         Loading code from disk
  306.  
  307.    SEE ALSO
  308.         exec/execbase.i, CacheControl, CacheClearE
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318. exec.library/CachePostDMA                           exec.library/CachePostDMA
  319.  
  320.    NAME
  321.         CachePostDMA - Take actions after to hardware DMA  (V37)
  322.  
  323.    SYNOPSIS
  324.         CachePostDMA(vaddress,&length,flags)
  325.                      a0       a1      d0
  326.  
  327.         CachePostDMA(APTR,LONG *,ULONG);
  328.  
  329.    FUNCTION
  330.         Take all appropriate steps after Direct Memory Access (DMA).  This
  331.         function is primarily intended for writers of DMA device drivers.  The
  332.         action will depend on the CPU type installed, caching modes, and the
  333.         state of any Memory Management Unit (MMU) activity.
  334.  
  335.         As implemented
  336.                 68000 - Do nothing
  337.                 68010 - Do nothing
  338.                 68020 - Do nothing
  339.                 68030 - Flush the data cache
  340.                 68040 - Flush matching areas of the data cache
  341.                 ????? - External cache boards, Virtual Memory Systems, or
  342.                         future hardware may patch this vector to best emulate
  343.                         the intended behavior.
  344.                         With a Bus-Snooping CPU, this function my end up
  345.                         doing nothing.
  346.  
  347.    INPUTS
  348.         address - Same as initially passed to CachePreDMA
  349.         length  - Same as initially passed to CachePreDMA
  350.         flags   - Values:
  351.                         DMA_NoModify - If the area was not modified (and
  352.                         thus there is no reason to flush the cache) set
  353.                         this bit.
  354.  
  355.    SEE ALSO
  356.         exec/execbase.i, CachePreDMA, CacheClearU, CacheClearE
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366. exec.library/CachePreDMA                             exec.library/CachePreDMA
  367.  
  368.    NAME
  369.         CachePreDMA - Take actions prior to hardware DMA  (V37)
  370.  
  371.    SYNOPSIS
  372.         paddress = CachePreDMA(vaddress,&length,flags)
  373.         d0                     a0       a1      d0
  374.  
  375.         APTR CachePreDMA(APTR,LONG *,ULONG);
  376.  
  377.    FUNCTION
  378.         Take all appropriate steps before Direct Memory Access (DMA).  This
  379.         function is primarily intended for writers of DMA device drivers.  The
  380.         action will depend on the CPU type installed, caching modes, and the
  381.         state of any Memory Management Unit (MMU) activity.
  382.  
  383.         This function supports advanced cache architectures that have
  384.         "copyback" modes.  With copyback, write data may be cached, but not
  385.         actually flushed out to memory.  If the CPU has unflushed data at the
  386.         time of DMA, data may be lost.
  387.  
  388.         As implemented
  389.                 68000 - Do nothing
  390.                 68010 - Do nothing
  391.                 68020 - Do nothing
  392.                 68030 - Do nothing
  393.                 68040 - Write any matching dirty cache lines back to memory.
  394.                         As a side effect of the 68040's design, matching data
  395.                         cache lines are also invalidated -- future CPUs may
  396.                         be different.
  397.                 ????? - External cache boards, Virtual Memory Systems, or
  398.                         future hardware may patch this vector to best emulate
  399.                         the intended behavior.
  400.                         With a Bus-Snooping CPU, this function my end up
  401.                         doing nothing.
  402.  
  403.    INPUTS
  404.         address - Base address to start the action.
  405.         length  - Pointer to a longword with a length.
  406.         flags   - Values:
  407.                         DMA_Continue - Indicates this call is to complete
  408.                         a prior request that was broken up.
  409.  
  410.    RESULTS
  411.         paddress- Physical address that coresponds to the input virtual
  412.                   address.
  413.         &length - This length value will be updated to reflect the contiguous
  414.                   length of physical memory present at paddress.  This may
  415.                   be smaller than the requested length.  To get the mapping
  416.                   for the next chunk of memory, call the function again with
  417.                   a new address, length, and the DMA_Continue flag.
  418.  
  419.    NOTE
  420.         Due to processor granularity, areas outside of the address range
  421.         may be affected by the cache flushing actions.  Care has been taken
  422.         to ensure that no harm is done outside the range, and that activities
  423.         on overlapping cache lines won't harm data.
  424.  
  425.    SEE ALSO
  426.         exec/execbase.i, CachePostDMA, CacheClearU, CacheClearE
  427.  
  428.